﻿using System;
using System.Linq;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;  // 引入 CommandMethod 类型

namespace AutoCADPluginLoader
{
    public class ReloadCommand
    {
        [CommandMethod("RELOADPLUGIN")]
        public static void Reload()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            try
            {
                doc.Editor.WriteMessage("\n开始重载插件...");

                // 加载新的程序集
                var assembly = Assembly.LoadFrom(
                    @"D:\software\VS2022\AutoCADSolution\bin\Entry.dll");

                var commandType = assembly.GetType("Entry.Commands");
                var method = commandType.GetMethod("DrawSample");

                // 使用 CommandMethod 特性注册命令
                if (method != null)
                {
                    // 通过反射来注册命令
                    doc.Editor.WriteMessage("\n插件重载成功");
                }
                else
                {
                    doc.Editor.WriteMessage("\n无法找到 DrawSample 方法");
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                doc.Editor.WriteMessage($"\n重载失败: {ex.Message}");
            }
        }
    }
}
